package com.meihua.shellapp.activity.set;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.meihua.shellapp.R;
import com.meihua.shellapp.activity.login.LoginActivity;
import com.meihua.shellapp.activity.webview.WebActivity;
import com.meihua.shellapp.adapter.personalcenter.SetUpAdapter;
import com.meihua.shellapp.base.BaseActivity;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.common.Constant;
import com.meihua.shellapp.dialog.BaseMessageDialog;
import com.meihua.shellapp.utils.CleanUtils;
import com.meihua.shellapp.utils.SharedPreferencesUtil;
import com.meihua.shellapp.utils.ToastUtils;
import com.meihua.shellapp.utils.URL;
import com.meihua.shellapp.utils.UrlUtils;
import com.meihua.shellapp.utils.ViewUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 作者：lc on 2017/9/26.
 * 邮箱：lc0607live@163.com
 */

public class SetUpActivity extends BaseActivity {

    @Bind(R.id.lv_setup)
    ListView lv_setup;//设置中的板块集合
    @Bind(R.id.iv_top_left)
    ImageView iv_top_left;//返回
    @Bind(R.id.tv_title)
    TextView tv_title;//标题
    @Bind(R.id.bt_loginout)
    Button bt_loginout;//退出登录
    @Bind(R.id.VersionName)
    TextView VersionName;//当前版本号

    private SetUpAdapter adapter;//板块数据适配器
    private List<String> mDataStr;//板块数据
    private BaseMessageDialog dialog;


    @Override
    public int getLayoutId() {
        return R.layout.activity_setup;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        iv_top_left.setVisibility(View.VISIBLE);
        tv_title.setText("设置");
        tv_title.setVisibility(View.VISIBLE);
        mDataStr = new ArrayList<>();
        mDataStr.add("账户与安全");
        mDataStr.add("清除缓存");
        mDataStr.add("关于我们");
        VersionName.setText("当前版本号:"+ UrlUtils.getAppVersionName(this));
        adapter = new SetUpAdapter(mContext, mDataStr);
        lv_setup.setAdapter(adapter);
        iv_top_left.setOnClickListener(this);
        lv_setup.setOnItemClickListener(mItemClick);
        bt_loginout.setOnClickListener(this);
        ViewUtils.getInstance().setHeightByListView(lv_setup);
    }

    /**
     * 单项点击事件
     */
    private AdapterView.OnItemClickListener mItemClick = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Intent in = new Intent();
            switch (i) {
                case 0://账户与安全
                    in.setClass(mContext, AccountSecurityActivity.class);
                    startActivity(in);
                    break;
                case 1://清除缓存
                    dialog = new BaseMessageDialog.Builder(mContext)
                            .setClickSure(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    boolean b = CleanUtils.cleanInternalCache();
                                    ToastUtils.getInstance().showToast(b ? "清除缓存成功" : "清除缓存失败");
                                    dialog.dismiss();
                                }
                            })
                            .setMessage("确定清除缓存吗？")
                            .show();

                    break;
                case 2://关于我们

                    in.setClass(mContext, WebActivity.class);
                    in.putExtra("url", URL.getUrl().WEBABOUTUS);
                    startActivity(in);
                    break;
            }
        }
    };

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.bt_loginout://退出登录
                dialog = new BaseMessageDialog.Builder(mContext)
                        .setClickSure(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                MyApplication.getInstance().bean = null;
                                SharedPreferencesUtil.getInstance().saveString(mContext, "passWord", "");
                                SharedPreferencesUtil.getInstance().saveBoolean(mContext, Constant.ISLOGIN, false);
                                startActivity(new Intent(mContext, LoginActivity.class));
                                finish();
                                dialog.dismiss();
                            }
                        })
                        .setMessage("您确定要退出登录？")
                        .show();
                break;
        }
    }
}
